package com.cencat.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cencat.user.entity.UserPermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户权限Mapper接口
 * @author cencat
 * @since 2024-01-01
 */
@Mapper
public interface UserPermissionMapper extends BaseMapper<UserPermission> {

    /**
     * 根据用户ID查询用户权限关联
     * @param userId 用户ID
     * @param tenantId 租户ID
     * @return 用户权限关联列表
     */
    @Select("SELECT * FROM user_permission WHERE user_id = #{userId} AND tenant_id = #{tenantId}")
    List<UserPermission> selectByUserId(@Param("userId") Long userId, @Param("tenantId") Long tenantId);

    /**
     * 根据权限ID查询用户权限关联
     * @param permissionId 权限ID
     * @param tenantId 租户ID
     * @return 用户权限关联列表
     */
    @Select("SELECT * FROM user_permission WHERE permission_id = #{permissionId} AND tenant_id = #{tenantId}")
    List<UserPermission> selectByPermissionId(@Param("permissionId") Long permissionId, @Param("tenantId") Long tenantId);

    /**
     * 根据用户ID删除用户权限关联
     * @param userId 用户ID
     * @param tenantId 租户ID
     * @return 删除数量
     */
    @Select("DELETE FROM user_permission WHERE user_id = #{userId} AND tenant_id = #{tenantId}")
    int deleteByUserId(@Param("userId") Long userId, @Param("tenantId") Long tenantId);

    /**
     * 根据权限ID删除用户权限关联
     * @param permissionId 权限ID
     * @param tenantId 租户ID
     * @return 删除数量
     */
    @Select("DELETE FROM user_permission WHERE permission_id = #{permissionId} AND tenant_id = #{tenantId}")
    int deleteByPermissionId(@Param("permissionId") Long permissionId, @Param("tenantId") Long tenantId);

    /**
     * 批量删除用户权限
     * @param userId 用户ID
     * @param permissionIds 权限ID列表
     * @param tenantId 租户ID
     * @return 删除数量
     */
    int batchDeleteUserPermissions(@Param("userId") Long userId, @Param("permissionIds") List<Long> permissionIds, @Param("tenantId") Long tenantId);

}